This notebook visualizes stop and moving points identified from the data of 900+ cargo drivers. The focus of this notebook is in:
I used the following criteria to classify stop points. The rest are moving points.
After finding stop points, I further processed the data via the next two steps:
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings(action='ignore')
df = pd.read_csv('../data/data_with_sp.csv')
pd.set_option('max_colwidth',10)
city_df = pd.read_csv('../data/cargo_cities.csv')
#print("\nPreview data and city coordinates: ")
#print(df.head(3))
#print(city_df.head(3))
# Helper script
import visualize as vis
mpl.rcParams['figure.dpi'] = 1000
# List of available plots: ['stop_move_basic', 'move_allspeed', 'move_speed100', 'move_timeofDay', 'stop_timeofDay']
vis.show_plot('stop_move_basic',df,city_df)
vis.show_plot('move_allspeed',df,city_df)
vis.show_plot('stop_timeofDay',df,city_df)
vis.show_plot('move_timeofDay',df,city_df)
plt.style.use('ggplot')
import seaborn as sns
def std_plot(data,xlabel,ylabel='Number of Points',legend=False,plotstyle='bar',hist_max=20,bins=100,figscale=1):
fig = plt.figure(figsize=(7*figscale,3*figscale))
ax = plt.axes()
if plotstyle=='bar':
data.plot.bar(ax=ax,legend=legend)
elif plotstyle=='hist':
plt.hist(data,bins=bins,range=(data.min(),hist_max))
ax.set_xlabel(xlabel,fontsize='small',fontweight='bold')
if ylabel is not None: ax.set_ylabel(ylabel,fontsize='small',fontweight='bold')
ax.xaxis.set_tick_params(labelsize='small')
ax.yaxis.set_tick_params(labelsize='small')
plt.tight_layout()
plt.show()
df['hour'] = pd.to_datetime(df['datetime']).dt.hour
totals = df[df.stop_pt.notnull()].hour.value_counts(sort=False).to_frame('total count')
percentages = df[df.stop_pt.notnull()].hour.value_counts(sort=False,normalize=True).to_frame('relative').round(3)
#print(pd.concat([totals,percentages],axis=1))
std_plot(totals,'Hour')
Note: This plot shows the first hour of stopping.
max_dur = 7
totals = df[df['stop_duration']>max_dur].hour.value_counts(sort=False).to_frame('total count')
totals = totals.sort_index(ascending=True)
percentages = df[df.stop_duration>max_dur].hour.value_counts(sort=False,normalize=True).to_frame('relative')
std_plot(totals,'Hour')
totals = df[df.stop_pt.isnull()].hour.value_counts(sort=False).to_frame('total count')
percentages = df[df.stop_pt.isnull()].hour.value_counts(normalize=True).to_frame('relative').round(3)
std_plot(totals,'Hour')
duration = df[df.stop_pt.notnull()].stop_duration
std_plot(duration,'Hour',plotstyle='hist',hist_max=15)
speed = df[df.stop_pt.isnull()].avgspeed
std_plot(speed,'Speed (kmph)',plotstyle='hist',hist_max=300,bins=100,figscale=1.25)
# To hide/show Code
from IPython.display import HTML
toggl = '''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Show/Hide Python Code"></form>'''
HTML(toggl)
# Show/Hide Warnings
from IPython.display import HTML
def show_hide_warnings():
return '''<script>
code_show_err=false;
function code_toggle_err() {
if (code_show_err){
$('div.output_stderr').hide();
} else {
$('div.output_stderr').show();
}
code_show_err = !code_show_err
}
$( document ).ready(code_toggle_err);
</script>
<a href="javascript:code_toggle_err()">Show/Hide Warnings</a>'''
#HTML(show_hide_warnings())
# Styling for Notebook
from IPython.core.display import HTML,Javascript
def css():
style = open("css/custom.css", "r").read()
return HTML(style)
css()